﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GraphicsFramework.Entity
{
    public class Rectangle2F
    {
        private Point2F point1;
        private Point2F point2;

        public Rectangle2F()
        {
            point1 = new Point2F();
            point2 = new Point2F();
        }

        public Rectangle2F(Entity.Point2F point1, Entity.Point2F point2)
        {
            float minx;
            float miny;
            float maxx;
            float maxy;

            if (point1.X < point2.X)
            {
                minx = point1.X;
                maxx = point2.X;
            }
            else
            {
                minx = point2.X;
                maxx = point1.X;
            }

            if (point1.Y < point2.Y)
            {
                miny = point1.Y;
                maxy = point2.Y;
            }
            else
            {
                miny = point2.Y;
                maxy = point1.Y;
            }

            this.point1 = new Point2F(minx, miny);
            this.point2 = new Point2F(maxx, maxy);

        }

        public Rectangle2F(Entity.Line2F line)
        {
            float minx;
            float miny;
            float maxx;
            float maxy;

            if (line.Point1.X < line.Point2.X) 
            { 
                minx = line.Point1.X; 
                maxx = line.Point2.X; 
            }
            else 
            { 
                minx = line.Point2.X; 
                maxx = line.Point1.X; 
            }

            if (line.Point1.Y < line.Point2.Y) 
            { 
                miny = line.Point1.Y; 
                maxy = line.Point2.Y; 
            }
            else 
            { 
                miny = line.Point2.Y; 
                maxy = line.Point1.Y; 
            }

            point1 = new Point2F(minx, miny);
            point2 = new Point2F(maxx, maxy);
        }

        public Rectangle2F Clone()
        {
            return new Rectangle2F(point1.Clone(), point2.Clone());
        }

        public void Merge(Rectangle2F rect)
        {
            if (rect != null)
            {
                if (point1.X > rect.Point1.X)
                    point1.X = rect.Point1.X;

                if (point2.X < rect.Point2.X)
                    point2.X = rect.Point2.X;

                if (point1.Y > rect.Point1.Y)
                    point1.Y = rect.Point1.Y;

                if (point2.Y < rect.Point2.Y)
                    point2.Y = rect.Point2.Y;
            }
        }

        public bool IsInside(float x, float y)
        {
            if ((point1.X <= x) && (point2.X >= x))
                if ((point1.Y <= y) && (point2.Y >= y))
                    return true;

            return false;
        }

        public Point2F GetCenter()
        {
            return new Point2F((point1.X + point2.X) / 2F, (point1.Y + point2.Y) / 2F);
        }

        public Point2F Point1
        {
            get { return point1; }
            set { point1 = value; }
        }

        public Point2F Point2
        {
            get { return point2; }
            set { point2 = value; }
        }

        public float Width
        {
            get { return Math.Abs(point2.X - point1.X);  }
        }

        public float Height
        {
            get { return Math.Abs(point2.Y - point1.Y); }
        }

        public override string ToString()
        {
            return string.Format("Point1 [X = {0}, Y = {1}]; Point2 [X = {2}, Y = {3}]", point1.X, point1.Y, point2.X, point2.Y);
        }

    }
}
